/**
 * This code was written as a simple 3D example application for 
 * Android devices. It may be used in the practical exercise
 * "Praktikum Computerspiele/Special Effects" but it is not 
 * allowed to be distributed further.
 * 
 * No warranties, all rights reserved.
 * 
 * Author: Dr. Philipp Jenke
 * philipp.jenke@uni-tubingen.de
 * T?bingen, October, 28th 2012
 */

package com.curluin.view;

import com.curluin.controll.Controller;
import com.curluin.inputInterpretation.TouchInterpreter;
import com.curluin.math.MyVector;
import com.curluin.rendering.Renderer3D;
import com.curluin.scene.Node;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.WindowManager;


/**
 * This class represents a view for 3D content
 * 
 * @author Philipp Jenke
 * 
 */
public class Interactor extends GLSurfaceView {

	/**
	 * Controller
	 */
	private Controller controller;
	
	/**
	 * Renderer object
	 */
	private Renderer3D renderer3d;

	/**
	 * Last coordinates of the mouse
	 */
	private MyVector lastMouseCoordinates = MyVector.makeVector2(-1, -1);

	/**
	 * Camera object.
	 */
	public Camera camera;

	/**
	 * Abstraction of the camera control
	 */
	public CameraController cameraController;
	
	/**
	 * Touch interpreter module
	 */
	private TouchInterpreter touchInterpreter;
	
	/**
	 * Width of display
	 */
	public static int screenWidth;
	
	/**
	 * Height of display
	 */
	public static int screenHeight;

	/**
	 * Constructor
	 * 
	 * @param context
	 */
	public Interactor(Context context) {
		super(context);
		// Context and controller are the same in our case
		controller = (Controller) context;
		
		// Get display size
		DisplayMetrics displayMetrics = new DisplayMetrics();
		WindowManager wm = (WindowManager) controller.getApplicationContext().getSystemService(Context.WINDOW_SERVICE); // the results will be higher than using the activity context object or the getWindowManager() shortcut
		wm.getDefaultDisplay().getMetrics(displayMetrics);

		//disable automatic standby
		setKeepScreenOn(true);
		
		int widthCorrection = 53;
		screenWidth = displayMetrics.widthPixels+widthCorrection;
		screenHeight = displayMetrics.heightPixels;
		Controller.visibleWidth = (screenWidth)/8.0f;
		Controller.visibleLength = screenHeight/8.0f;		
		Controller.xModelRange = Controller.visibleWidth;
		Controller.zModelRange = Controller.visibleLength * 5;		
		
		camera = new Camera();
		
		cameraController = new CameraController(camera);
		
		touchInterpreter = new TouchInterpreter(controller, screenWidth, screenHeight);
		
		renderer3d = new Renderer3D(camera);
		setRenderer(renderer3d);
	}

	/*
	 * Event-Handler: user moved the finger over the view. (non-Javadoc)
	 * 
	 * @see android.view.View#onTouchEvent(android.view.MotionEvent)
	 */
	@Override
	public boolean onTouchEvent(final MotionEvent event) {
		
		if(!Controller.SCENE_ROTATABLE){
			touchInterpreter.handleEvent(event);
		}
		else{
			// Original
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				lastMouseCoordinates.set(0, event.getX());
				lastMouseCoordinates.set(1, event.getY());
			} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
				if ((lastMouseCoordinates.get(0) > 0)
						&& (lastMouseCoordinates.get(1) > 0)) {
					cameraController
					.mouseDeltaX(event.getX() - lastMouseCoordinates
							.get(0));
					cameraController
					.mouseDeltaY(event.getY() - lastMouseCoordinates
							.get(1));
				}
				lastMouseCoordinates.set(0, event.getX());
				lastMouseCoordinates.set(1, event.getY());
			} else if (event.getAction() == MotionEvent.ACTION_UP) {
				lastMouseCoordinates = MyVector.makeVector2(event.getX(),
						event.getY());
			}
		}
		return true;
	}

	/**
	 * Setter.
	 * 
	 * @param scene
	 */
	public void setScene(Node scene) {
		renderer3d.setScene(scene);
	}


	public void destroy() {
		this.destroyDrawingCache();
		lastMouseCoordinates=null;
		camera=null;
		cameraController=null;
		touchInterpreter.destroy();
		touchInterpreter=null;
//		rootNode.destroy();
//		rootNode =null;
		renderer3d.destroy();
		renderer3d=null;
	}

	public void addToRootNode(Node node) {
		renderer3d.rootNode.addChild(node);
	}

	
}
